<?php

namespace model\repo;

use api\lib\ErrorLog;

class DropCatch extends Model
{
    use ErrorLog;

    protected $table = "dropcatch";

    protected $pk = "_id";

    public function __construct()
    {
        $config = parse_ini_file(dirname(__FILE__).'/../../conf/config.ini');
        parent::__construct($config['host'], $config['user'], $config['password'], $config['database'], 3306);
    }

    public function updateDomain(array $diff, /* int */ $_id) {
        $_id = intval($_id);
        return $this->update($diff, sprintf("`%s`=%d", $this->pk, $_id));
    }

    /**
     * 根据域名找到记录
     * @param $domain
     * @return array
     */
    public function findByDomain(/*string */$domain)
    {
        return $this->queryGetRow($this->table, "domain='".addslashes($domain)."'");
    }

    public function create(/* array */$assoc) {
        return $this->insertGetId($this->table, $assoc);
    }

    public function getAll(/* array */$where) {
        return $this->queryGetAll($this->table, implode(" AND ", $where));
    }

    public function paginate(/* array */$where, /* int */$page, /* int */$size) {
        $sWhere = implode(" AND ", $where);
        $total = $this->getTotal($this->table, $sWhere);
        $totalPage = ceil($total / $size);
        if ($page < 1) {$page = 1;}
        if ($totalPage > 0 && $page > $totalPage) {$page = $totalPage;}
        $limit = sprintf("%d,%d", ($page-1)*$size, $size);
        $data = $this->queryGetAll($this->table, $sWhere, $limit);

        return array(
            "total" => $total,
            "total_page" => $totalPage,
            "data" => $data,
            "current_page" => $page,
            "page_size" => $size
        );
    }
}